<template>
  <el-dialog v-model="dialogVisible" title="个人信息" width="500px" draggable>
    <div class="myinfobox">
      <div>
        <el-avatar shape="circle" :size="100" :src="headUrl" />
      </div>
      <div class="scbox">
        <el-upload
          v-model:file-list="fileListzy"
          class="upload-demo"
          multiple
          :limit="1"
          :http-request="uploadFile"
          :show-file-list="showFileList"
        >
          <el-button type="primary">上传头像</el-button>
          <template #tip>
            <div class="el-upload__tip"></div>
          </template>
        </el-upload>
      </div>
      <div>用户名称:{{ useInfo.userName }}</div>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive } from "vue";
import { uploadhead } from "@/api/system/system.js";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRoute } from "vue-router";
let fileListzy = ref([]); // 图片列表
let imgsrcList = ref([]); // 图片列表
const useInfo = JSON.parse(sessionStorage.getItem("useInfo"));
const dialogVisible = ref(false);
let headUrl = ref(useInfo.avatar); // 头像地址
let showFileList = false;
const openDialog = () => {
  dialogVisible.value = true;
};
let uploadFile = val => {
  let fd = new FormData();
  fd.append("avatarfile", val.file);
  uploadhead(fd).then(res => {
    if (res.code == 200) {
      headUrl.value = res.imgUrl;
      imgsrcList.value[0] = res.imgUrl;
      ElMessage.success("上传成功");
    }
  });
};

defineExpose({ openDialog });
</script>
<style scoped>
.myinfobox {
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.scbox {
  margin-top: 10px;
}
</style>
